package dk.itu.spvc.tourtracker.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.text.Html.TagHandler;
import android.util.Log;

public class JSONServerResponse {

	private boolean hasError = false;
	private String message = "";
	private String action = "";
	private JSONObject object = null;

	public JSONServerResponse(HttpResponse response) {
		try {
			JSONObject object = (JSONObject) new JSONTokener(EntityUtils.toString(response.getEntity())).nextValue();
			this.hasError = (object.getString("error").contentEquals("0")) ? false : true;
			this.message = object.getString("message");
			this.action = object.getString("action");
			this.object = object;
		} catch (Exception e) {
			this.hasError = true;
			this.message = "Exception occured during JSON parsing.";
		}
	}

	public boolean hasError() {
		return this.hasError;
	}

	public String getMessage() {
		return this.message;
	}

	public String getAction() {
		return this.action;
	}

	public JSONObject getObject() {
		return this.object;
	}

	public List<Trip> getTrips() {

		List<Trip> trips = null;

		if (this.getAction().contentEquals("get_trips")) {

			trips = new ArrayList<Trip>();

			try {

				JSONArray json_trips = this.object.getJSONArray("trips");

				for (int i = 0; i < json_trips.length(); i++) {

					JSONObject json_trip = json_trips.getJSONObject(i);

					String tripRemoteID = json_trip.getString("id");
					String tripName = json_trip.getString("name");

					Trip newTrip = new Trip(tripName);
					newTrip.setTripRemoteId(Integer.parseInt(tripRemoteID));

					trips.add(newTrip);
				}

			} catch (JSONException e) {
				return null;
			}

		}

		return trips;
	}

	public HashMap<Integer, Integer> getMappedTrips() {

		HashMap<Integer, Integer> mapping = null;

		if (this.getAction().contentEquals("upload_trip")) {

			mapping = new HashMap<Integer, Integer>();

			try {

				JSONArray json_mappings = this.object.getJSONArray("mappings");

				for (int i = 0; i < json_mappings.length(); i++) {

					JSONObject json_mapping = json_mappings.getJSONObject(i);

					int trip_local_id = json_mapping.getInt("trip_local_id");
					int trip_remote_id = json_mapping.getInt("trip_remote_id");

					mapping.put(trip_local_id, trip_remote_id);

				}

			} catch (JSONException e) {
				return null;
			}
		}

		return mapping;
	}

	public List<Point> getTripPathPoints() {

		List<Point> points = null;

		if (this.getAction().contentEquals("get_trip_data")) {

			points = new ArrayList<Point>();

			try {

				JSONArray json_pathpoints = this.object.getJSONArray("path_points");

				for (int i = 0; i < json_pathpoints.length(); i++) {

					JSONObject json_point = json_pathpoints.getJSONObject(i);
					Point p = new Point(json_point.getDouble("longitude"), json_point.getDouble("latitude"), PointType.PATH);
					points.add(p);
				}

			} catch (JSONException e) {
				return null;
			}

		}

		return points;

	}

	public List<NotePoint> getTripNotePoints() {

		List<NotePoint> points = null;

		if (this.getAction().contentEquals("get_trip_data")) {

			points = new ArrayList<NotePoint>();

			try {

				Log.d("TourTracker",this.object.toString());
				JSONArray json_pinpoints = this.object.getJSONArray("pin_points");
				

				for (int i = 0; i < json_pinpoints.length(); i++) {

					JSONObject json_point = json_pinpoints.getJSONObject(i);
					
					NotePoint p = new NotePoint(json_point.getDouble("longitude"), json_point.getDouble("latitude"), PointType.PIN, new Note(json_point.getString("note_text")));				
					points.add(p);
				}

			} catch (JSONException e) {
				return null;
			}

		}

		return points;

	}
	
	public Trip getTrip() {
		
		Trip trip = null;
		
		if (this.getAction().contentEquals("get_trip_data")) {
				
			try {
				
				trip = new Trip(this.getObject().getString("trip_name"));
				trip.setTripRemoteId(this.getObject().getInt("trip_id"));
				
			} catch (JSONException e) {
				return null;
			}
			
		}
		
		return trip;
		
	}

}
